package nie.dao;

import nie.bean.User;
import nie.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class UserDao {
  // 添加用户的操作
  public boolean insert(User user) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();

      // 发送SQL语句
      String sql = "insert into t_user(username,password,age,role,name) values(?,?,?,?,?)";

      ps = conn.prepareStatement(sql);
      ps.setString(1, user.getUsername());
      ps.setString(2, user.getPassword());
      ps.setInt(3, user.getAge());
      ps.setInt(4, user.getRole());
      ps.setString(5, user.getName());
      int num = ps.executeUpdate();
      return num > 0;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, ps, conn);
    }
    return false;
  }

  // 查询所有的User对象
  public ArrayList<User> findAll() {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    ArrayList<User> list = new ArrayList<User>();
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 获得Statement对象
      stmt = conn.createStatement();
      // 发送SQL语句
      String sql = "SELECT * FROM t_user";
      rs = stmt.executeQuery(sql);
      // 处理结果集
      while (rs.next()) {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setUsername(rs.getString("username"));
        user.setPassword(rs.getString("password"));
        user.setAge(rs.getInt("age"));

        list.add(user);
      }
      return list;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
    return null;
  }

  // 根据id查找指定的user
  public User find(int id) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 获得Statement对象
      stmt = conn.createStatement();
      // 发送SQL语句
      String sql = "SELECT * FROM t_user WHERE id=" + id;
      rs = stmt.executeQuery(sql);
      // 处理结果集
      while (rs.next()) {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setUsername(rs.getString("username"));
        user.setPassword(rs.getString("password"));
        user.setAge(rs.getInt("age"));
        user.setName(rs.getString("name"));

        return user;
      }
      return null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
    return null;
  }

  // 根据登录用户名查找指定的user
  public boolean findByUsername(String username) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 发送SQL语句
      String sql = "SELECT * FROM t_user WHERE username=?";
      // 获得Statement对象
      ps = conn.prepareStatement(sql);
      ps.setString(1, username);
      rs = ps.executeQuery();
      // 处理结果集
      return rs.next();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, ps, conn);
    }
    return false;
  }

  // 删除用户
  public boolean delete(int id) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 获得Statement对象
      stmt = conn.createStatement();
      // 发送SQL语句
      String sql = "DELETE FROM t_user WHERE id=" + id;
      int num = stmt.executeUpdate(sql);
      if (num > 0) {
        return true;
      }
      return false;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
    return false;
  }

  // 修改用户
  public boolean update(User user) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();

      // 发送SQL语句

      String sql = "update t_user set username=?,password=?,age=? where id=?";
      ps = conn.prepareStatement(sql);
      ps.setString(1, user.getUsername());
      ps.setString(2, user.getPassword());
      ps.setInt(3, user.getAge());
      ps.setInt(4, user.getId());
      int num = ps.executeUpdate();
      if (num > 0) {
        return true;
      }
      return false;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, ps, conn);
    }
    return false;
  }

  public User checkUser(User user) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 发送SQL语句
      String sql = "SELECT * FROM t_user WHERE username=? and password=? and role=?";
      // 获得Statement对象
      ps = conn.prepareStatement(sql);
      ps.setString(1, user.getUsername());
      ps.setString(2, user.getPassword());
      ps.setInt(3, user.getRole());
      rs = ps.executeQuery();
      // 处理结果集
      while (rs.next()) {
        User target = new User();
        target.setId(rs.getInt("id"));
        target.setUsername(rs.getString("username"));
        target.setPassword(rs.getString("password"));
        target.setName(rs.getString("name"));
        target.setAge(rs.getInt("age"));
        target.setRole(rs.getInt("role"));
        return target;
      }
      return null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, ps, conn);
    }
    return null;
  }
}